package cn.ctyun.xstore.s3.api.object;

import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;

import cn.ctyun.xstore.s3.client.AwsS3ClientBuilder;

import java.io.IOException;
import java.io.InputStream;


public class UploadObject {
    public static void putObject(String bucketName, String keyName, InputStream is,AccessControlList acl, AmazonS3 s3) {
        ObjectMetadata metadata = new ObjectMetadata();
        try {
            metadata.setContentLength(is.available());//在请求头部header的Content-Length属性设置文件长度
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, is, metadata);
            if (acl != null) {
                putObjectRequest.setAccessControlList(acl);
            }
            PutObjectResult result = s3.putObject(putObjectRequest);
            System.out.println(result.getETag());
        } catch (AmazonServiceException e) {
            System.err.println(e.getErrorMessage());
        }
        System.out.println("Done!");
    }
}
